package com.example.xianji.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.adapter.My_TiWenAdapter;
import com.example.xianji.BGARefresh.BGANormalRefreshViewHolder;
import com.example.xianji.BGARefresh.BGARefreshLayout;
import com.example.xianji.BGARefresh.BGARefreshLayout.BGARefreshLayoutDelegate;
import com.example.xianji.bean.My_DuiJie;

import com.example.xianji.constant.Constant;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Main_My_TiWenActivity extends Activity implements
        BGARefreshLayoutDelegate {
    /********************** 我的提问 **************************/
    private RelativeLayout my_tiwen_back;
    //    private SharedPreferences sp;
//    private Editor ed;
    private ArrayList<My_DuiJie> list = new ArrayList<My_DuiJie>();
    private My_TiWenAdapter mMy_TiWenAdapter;
    private BGARefreshLayout tiwen_PullToRefreshLayout_two;
    private ListView tiwen_mylistview;
    private int page = 1;

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    tiwen_mylistview.setAdapter(mMy_TiWenAdapter);
                    tiwen_PullToRefreshLayout_two.endRefreshing();
                    break;
                case 2:
                    mMy_TiWenAdapter.notifyDataSetChanged();
                    tiwen_PullToRefreshLayout_two.endLoadingMore();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__my__ti_wen);

        my_tiwen_back = (RelativeLayout) findViewById(R.id.my_tiwen_back);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        tiwen_PullToRefreshLayout_two = (BGARefreshLayout) findViewById(R.id.tiwen_PullToRefreshLayout_two);
        tiwen_mylistview = (ListView) findViewById(R.id.tiwen_mylistview);
        mMy_TiWenAdapter = new My_TiWenAdapter(this, list);
        my_tiwen_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_My_TiWenActivity.this.finish();
            }
        });
        tiwen_PullToRefreshLayout_two.setDelegate(this);
        tiwen_PullToRefreshLayout_two.setRefreshViewHolder(new BGANormalRefreshViewHolder(
                getApplicationContext(), true));
        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                get_tiwen();
            }
        });
    }

    public void get_tiwen() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        parms.addBodyParameter("page", 1 + "");
        httpUtils.send(HttpMethod.POST, Contant.my_tiwen, parms,
                new RequestCallBack<String>() {
                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        tiwen_PullToRefreshLayout_two
                                .endRefreshing();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            JSONObject response = new JSONObject(arg0.result);
                            if (response.getInt("status") == 1) {
                                JSONArray array = response.getJSONArray("data");

                                list.clear();
                                if (array.length() == 0) {
                                    Toast.makeText(Main_My_TiWenActivity.this,
                                            "您还没有提问哦", Toast.LENGTH_LONG).show();
                                } else {
                                    for (int i = 0; i < array.length(); i++) {
                                        JSONObject obj = array.getJSONObject(i);
                                        My_DuiJie mMy_DuiJie = new My_DuiJie();
                                        mMy_DuiJie.setTitle(obj
                                                .getString("title"));
                                        mMy_DuiJie.setStatus(obj
                                                .getInt("status"));

                                        mMy_DuiJie.setCreated_at(obj
                                                .getString("created_at"));
                                        mMy_DuiJie.setId(obj.getInt("id"));

                                        list.add(mMy_DuiJie);
                                    }
                                }
                                handler.sendEmptyMessage(1);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public void get_tiwen_more() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams parms = new RequestParams();
        parms.addBodyParameter("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        parms.addBodyParameter("page", page + "");
        httpUtils.send(HttpMethod.POST, Contant.my_tiwen, parms,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        tiwen_PullToRefreshLayout_two
                                .endLoadingMore();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        try {
                            JSONObject response = new JSONObject(arg0.result);
                            if (response.getInt("status") == 1) {
                                JSONArray array = response.getJSONArray("data");
                                if (array.length() == 0) {
                                    Toast.makeText(Main_My_TiWenActivity.this,
                                            "没有更多数据", Toast.LENGTH_LONG).show();
                                } else {
                                    for (int i = 0; i < array.length(); i++) {
                                        JSONObject obj = array.getJSONObject(i);
                                        My_DuiJie mMy_DuiJie = new My_DuiJie();
                                        mMy_DuiJie.setTitle(obj
                                                .getString("title"));
                                        mMy_DuiJie.setStatus(obj
                                                .getInt("status"));

                                        mMy_DuiJie.setCreated_at(obj
                                                .getString("created_at"));
                                        mMy_DuiJie.setId(obj.getInt("id"));
                                        list.add(mMy_DuiJie);
                                    }
                                }
                                handler.sendEmptyMessage(2);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }


    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        page = 1;
        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                get_tiwen();
            }
        });
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(
            BGARefreshLayout refreshLayout) {
        page++;
        XJApplication.cachThreadPool.execute(new Runnable() {

            @Override
            public void run() {
                get_tiwen_more();
            }
        });
        return true;
    }
}
